package com.ficus.road.maintain.strategy.operationlog;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.OperationLog;
import com.ficus.road.maintain.core.model.UpdateHitStatusRequest;
import com.ficus.road.maintain.operationlog.model.ZHCallbackRequest;
import com.ficus.road.maintain.operationlog.model.ZHCallbackResponse;
import org.apache.commons.lang3.ObjectUtils;

/**
 * @author xiawen yang
 * @date 2021/12/24 上午11:14
 */
public class ZHCallbackStrategy implements OperationLogStrategy {
    @Override
    public Object processRequestParam(String jsonString) {
        JSONArray jsonArray = JSONArray.parseArray(jsonString);
        String hitId = jsonArray.getString(0);
        UpdateHitStatusRequest updateHitStatusRequest = jsonArray.getObject(1, UpdateHitStatusRequest.class);
        ZHCallbackRequest zhCallbackRequest = ZHCallbackRequest.builder().hitId(hitId).updateHitStatusRequest(updateHitStatusRequest).build();
        return zhCallbackRequest;
    }

    @Override
    public Object processResponse(String returnString, OperationLog operationLog) {
        ZHCallbackResponse zhCallbackResponse = JSONObject.parseObject(returnString, ZHCallbackResponse.class);
        if (ObjectUtils.isNotEmpty(zhCallbackResponse) && !zhCallbackResponse.getStatusCodeValue().equals(200)) {
            operationLog.setErrorMsg(zhCallbackResponse.getStatusCode());
            operationLog.setType("error");
        }
        return zhCallbackResponse;
    }

}
